package com.interview.productsalesreports.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.interview.productsalesreports.entity.User;
import com.interview.productsalesreports.query.ProductSalesReportsConditionQuery;
import com.interview.productsalesreports.service.ProductSalesReportsService;

@Controller
@RequestMapping(value = "productsales_reports")
public class ProductSalesReportsController {
	@Resource
	ProductSalesReportsService productSalesReportsService;

	@RequestMapping(value = "page", method = RequestMethod.GET)
	public String page(ProductSalesReportsConditionQuery conditionQuery,
			ModelMap map) {

		Map resultsMap = productSalesReportsService.statisticsByYearAndMon(
				conditionQuery.getYear(), conditionQuery.getMon());

		map.put("list", resultsMap.get("list"));
		map.put("dateYear", resultsMap.get("dateYear"));
		map.put("dateMon", resultsMap.get("dateMon"));
		map.put("conditionQuery", conditionQuery);
		map.put("thatMonSum", resultsMap.get("thatMonSum"));
		map.put("thatYearSum", resultsMap.get("thatYearSum"));
		return "productsales_reports/page";
	}

	// ---------------------------------------------------------------
	@RequestMapping(value = "allYearAndMon", method = RequestMethod.GET)
	@ResponseBody
	public Object allYearAndMon() {
		Map map = productSalesReportsService.yearAndMonForMap();

		return map;
	}

	// ---------------------------------------------
	@RequestMapping(value = "testAjax", method = RequestMethod.GET)
	public String testAjax(ModelMap map) {
		return "productsales_reports/testAjax";
	}

	@RequestMapping(value = "formAjax", method = RequestMethod.POST)
	@ResponseBody
	public Object formAjax(User user, ModelMap map) {
		System.out.println(user.getName() + "-------------------------"
				+ user.getSex());
		return user;
	}

	@RequestMapping(value = "requestAjax", method = RequestMethod.POST)
	@ResponseBody
	public Object requestAjax(User user, ModelMap map) {
		System.out.println(user);
		return user;
	}

	// -----------------------------------------------------------------------------------
	@RequestMapping(value = "ListBody", method = RequestMethod.POST)
	@ResponseBody
	public Object ListBody(
			@RequestBody ProductSalesReportsConditionQuery conditionQuery,
			ModelMap map) {

		Map ListBody = productSalesReportsService.statisticsByYearAndMon(
				conditionQuery.getYear(), conditionQuery.getMon());
		
		return ListBody;
	}

}
